package cn.fzkj.inbox03;

import akka.actor.*;
import scala.concurrent.duration.Duration;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @ author mr
 * @ description
 * @ since 2024/12/2
 */
public class Main03 {


    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("inbox");
        ActorRef inboxTest = system.actorOf(Props.create(InboxTest.class), "InboxTest");

        Inbox inbox = Inbox.create(system);
        inbox.watch(inboxTest);//监听一个actor

        //通过inbox来发送消息
        inbox.send(inboxTest, InboxTest.Msg.WORKING);
        inbox.send(inboxTest, InboxTest.Msg.DONE);
        inbox.send(inboxTest, InboxTest.Msg.CLOSE);

        for (;;){
            try {
                Object receive = inbox.receive(Duration.create(1, TimeUnit.SECONDS));
                if(receive == InboxTest.Msg.CLOSE){//收到的inbox的消息
                    System.out.println("inboxTextActor is closing");
                }else if(receive instanceof Terminated){//中断 ，和线程一个概念
                    System.out.println("inboxTextActor is closed");
                    system.terminate();
                    break;
                }else {
                    System.out.println(receive);
                }
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
    }
}
